//
//  CZGilmmerController.swift
//  GZWeibo007
//
//  Created by mac on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
//控制器模型
enum CZComposeViewControllerType: Int {
    case Compose = 1
    case Repost = 2//转发
    case Comment = 3//评论
}
class CZGilmmerController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置view背景色
        view.backgroundColor = UIColor.whiteColor()
       
        prepareUI()
        
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    //微博模型
    var status:CZStatus?
    //创建控制器
    private var controllerType:CZComposeViewControllerType
    //初始化方法
    init(controllerType:CZComposeViewControllerType){
        self.controllerType=controllerType
        super.init(nibName: nil, bundle: nil)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //准备UI
    private func prepareUI(){
        setupTextView()
        setupNavigationBar()
    }
    //设置导航栏
    private func setupNavigationBar(){
        navigationItem.leftBarButtonItem=UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        
        setupTitleView()
    }
    
    ///设置导航栏的标题
    private func setupTitleView(){
        var prefix="转发微博"
        if controllerType==CZComposeViewControllerType.Repost{
            
        }else{
            prefix="评论微博"
        }
        
        //获取用户名称
        if let name=CZUserAccount.loadUserAccount()?.screen_name{
            let titleName = prefix + "\n" + name
            
            let attrString = NSMutableAttributedString(string: titleName)
            
            let lable = UILabel()
            
            lable.numberOfLines=0;
            lable.textAlignment=NSTextAlignment.Center
            lable.font=UIFont.systemFontOfSize(16)
            
            let nameRange=(titleName as NSString).rangeOfString(name)
            
            //设置文本属性
            attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
            attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
            
            lable.attributedText=attrString
            lable.sizeToFit()
            navigationItem.titleView=lable
        }else{
            //没有用户名
            navigationItem.title=prefix
        
        }
    }
    
    //设置textView的约束
    
    private func setupTextView(){
        //添加子控件
        view.addSubview(textView)
        
        //添加约束
        textView.ff_Fill(view)
    }
    
    func close(){
    //关闭键盘
    textView.resignFirstResponder()
    
    // 关闭sv提示
    SVProgressHUD.dismiss()
        
    dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    func sendStatus(){
        let text = textView.text;
        
        if text == nil || text == ""{
            SVProgressHUD.showInfoWithStatus("内容不能为空")
            return
        }
        
        
        if controllerType == CZComposeViewControllerType.Repost{
        
            SVProgressHUD.showInfoWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
            //实现转发的功能
            CZNetworkTool.sharedInstance.repostStatus(status!.id, status: text, finished: { (result, error) -> () in
                
               self.compose("转发", error: error, result: result)
                
            })
        }else if controllerType == CZComposeViewControllerType.Comment{
        //显示正在发送
            SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
            //实现评论的功能
          CZNetworkTool.sharedInstance.commentStatus(status!.id, comment: text, finished: { (result, error) -> () in
            self.compose("评论", error: error, result: result)
          })
           

            
        } else if controllerType == CZComposeViewControllerType.Compose {
        
            SVProgressHUD.showInfoWithStatus("正在发布微博", maskType: SVProgressHUDMaskType.Black)
            CZNetworkTool.sharedInstance.sendStatus(text, image: nil,finished: { (result, error) -> () in
               self.compose("发布", error: error, result: result)
            })
        }
        
    }
    
//MARK: - 抽取转发评论
    func compose(text:String, error:NSError?,result:[String : AnyObject]?)
    {
        if error != nil {
            print("\(text)错误\(error)")
            SVProgressHUD.showErrorWithStatus("网络不好", maskType: SVProgressHUDMaskType.Black)
            return
        }
        print("\(text)成功\(result)")
        self.close()
    }






    private lazy var textView: UITextView = {
        let textView = UITextView()
        
        // 当textView被拖动的时候就会将键盘退回,textView能拖动
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.font = UIFont.systemFontOfSize(18)
        textView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        textView.textColor = UIColor.blackColor()
        textView.bounces = true
        textView.alwaysBounceVertical = true
        
        return textView
    }()

    

}
